import router from "@/router/index";
import { useLoginUserStore } from "@/store/useLoginUserStore";
import message from "@arco-design/web-vue/es/message";

/**
 * 全局权限校验
 */
router.beforeEach(async (to, from, next) => {
  const url = to.fullPath;
  const loginUser = useLoginUserStore().loginUser;
  if (url.startsWith("/admin")) {
    if (
      !loginUser ||
      loginUser.userRole === 0 ||
      loginUser.username === "未登录"
    ) {
      message.error("没有权限");
      next(`/user/login?redirect=${url}`);
      return;
    }
  }
  next();
});
